#!/bin/bash
# 注意甄别,不是一定可用 使用前请确认你的cuda版本.
# 检查root权限
if [ "$(id -u)" -ne 0 ]; then
    echo "请使用sudo运行此脚本"
    exit 1
fi

# 安装基础依赖
apt-get update && apt-get install -y \
    build-essential \
    cmake \
    git \
    libopencv-dev \
    wget

# CUDA 11.8安装
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sh cuda_11.8.0_520.61.05_linux.run --silent --toolkit

export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

# OpenVINO 2023.3安装
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB

echo "deb https://apt.repos.intel.com/openvino/2023 ubuntu20 main" > /etc/apt/sources.list.d/intel-openvino-2023.list
apt-get update && apt-get install -y openvino-2023.3.0

# TensorRT 8.6安装
apt-get install -y libnvinfer8 libnvinfer-plugin8 libnvparsers8 libnvonnxparsers8 libnvinfer-bin

# 配置环境变量
cat << EOF >> /etc/profile.d/yolov8_env.sh
export OPENVINO_HOME=/opt/intel/openvino_2023.3.0
export TENSORRT_DIR=/usr/lib/x86_64-linux-gnu
export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$OPENVINO_HOME/runtime/lib/intel64:\$TENSORRT_DIR
EOF

# 创建符号链接
ln -s /opt/intel/openvino_2023.3.0 /opt/intel/openvino

# 验证安装
source /etc/profile.d/yolov8_env.sh
nvcc --version | grep "release 11.8"
/opt/intel/openvino/setupvars.sh

echo "环境配置完成！请重新登录或执行：source /etc/profile.d/yolov8_env.sh"